package com.sihan.swvp.service.sjfx;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sihan.swvp.common.CommonUtil;
import com.sihan.swvp.dao.worksy.WorksyDao;

@Service
public class KshzsService{

	@Autowired
	private WorksyDao worksyDao;
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	public Map<String, Object> getCountByWorksy(Map<String, Object> params) {
		Date now = new Date(); 
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.add(Calendar.DATE, 1);//加一天是用于sql结束时间查询
		String todayTime= dateFormat.format(now); 
		
		String weekStartTime=getCurrentMonday();//当前周一时间
		String weekEndTime=getPreviousSunday();//当前周末时间
		String monthStartTime=getMinMonthDate(todayTime);//当前月初时间
		String monthEndTime=getMaxMonthDate(todayTime);//当前月末时间
		params.put("todayTime", todayTime);
		params.put("todayTimeEnd", (new SimpleDateFormat("yyyy-MM-dd")).format(cal.getTime()));
		params.put("weekStartTime", weekStartTime);
		params.put("weekEndTime", weekEndTime);
		params.put("monthStartTime", monthStartTime);
		params.put("monthEndTime", monthEndTime);
		return worksyDao.getCountByWorksy(params);
	}
	
	public Map<String, Object> getCountByWorkAndPatrol(Map<String, Object> params) {
		Date now = new Date(); 
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.add(Calendar.DATE, 1);//加一天是用于sql结束时间查询
		String todayTime= dateFormat.format(now); 
		
		String startTime="";
		String endTime="";
		
		if(CommonUtil.o2int(params.get("variable")) ==1){//每天起始结束时间
			startTime=todayTime;
			endTime=(new SimpleDateFormat("yyyy-MM-dd")).format(cal.getTime());
		}else if(CommonUtil.o2int(params.get("variable")) ==2){//每周起始结束时间
			startTime=getCurrentMonday();//当前周一时间
			endTime=getPreviousSunday();//当前周末时间
		}else if(CommonUtil.o2int(params.get("variable")) ==3){//每月起始结束时间
			startTime=getMinMonthDate(todayTime);//当前月初时间
			endTime=getMaxMonthDate(todayTime);//当前月末时间
			
		}
		
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		
		return worksyDao.getCountByWorkAndPatrol(params);
	}	
	
	public List<Map<String, Object>> getTodayWcShu(Map<String, Object> params) {
		Date now = new Date(); 
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.add(Calendar.DATE, 1);//加一天是用于sql结束时间查询
		String todayTime= dateFormat.format(now); 
		params.put("todayTime", todayTime);
		params.put("todayTimeEnd", (new SimpleDateFormat("yyyy-MM-dd")).format(cal.getTime()));

		return worksyDao.getTodayWcShu(params);
	}
	
	 // 获得当前日期与本周一相差的天数
    private static  int getMondayPlus() {
        Calendar cd = Calendar.getInstance();
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) {
            return -6;
        } else {
            return 2 - dayOfWeek;
        }
    } 
    // 获得当前周- 周一的日期
    private static  String getCurrentMonday() {
        int mondayPlus = getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus);
        Date monday = currentDate.getTime();
        DateFormat df = DateFormat.getDateInstance();
        String preMonday = df.format(monday);
        return preMonday;
    }
    
    // 获得当前周- 周日  的日期
    private static String getPreviousSunday() {
        int mondayPlus = getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus +6);
        Date monday = currentDate.getTime();
        DateFormat df = DateFormat.getDateInstance();
//    	String preMonday = df.format(monday);
        
        /**
         * 加一天是用于sql结束时间查询
         */
    	Calendar cal = Calendar.getInstance();
		cal.setTime(monday);
		cal.add(Calendar.DATE, 1);
		
		String preMonday = df.format(cal.getTime());
		
        return preMonday;
    }






    // 获得当前月--开始日期
    public static  String getMinMonthDate(String date) {   
             Calendar calendar = Calendar.getInstance();   
              try {
                 calendar.setTime(dateFormat.parse(date));
                 calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); 
                 return dateFormat.format(calendar.getTime());
               } catch (java.text.ParseException e) {
               e.printStackTrace();
              }
            return null;
    }



// 获得当前月--结束日期
    public static  String getMaxMonthDate(String date){   
         Calendar calendar = Calendar.getInstance();   
         try {
                calendar.setTime(dateFormat.parse(date));
                calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
                
                
                /**
                 * 加一天是用于sql结束时间查询
                 */
                calendar.add(Calendar.DATE, 1);
                
                return dateFormat.format(calendar.getTime());
         }  catch (java.text.ParseException e) {
                e.printStackTrace();
          }
         
        return null;
}
	
}